package cn.stylefeng.guns.modular.fi.controller;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiTempCarAppStand;
import cn.stylefeng.guns.modular.fi.model.params.FiTempCarAppStandParam;
import cn.stylefeng.guns.modular.fi.service.FiTempCarAppStandService;
import cn.stylefeng.guns.modular.transport.entity.TDispatch;
import cn.stylefeng.guns.modular.transport.model.params.TransportDispatchParam;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportDispatchService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 临时车超额审批标准控制器
 *
 * @author zhangzh
 * @Date 2024-11-19 10:32:33
 */
@Controller
@RequestMapping("/fiTempCarAppStandExa")
public class FiTempCarAppStandExaController extends BaseController {

    private String PREFIX = "/fi/fiTempCarAppStandExa";

    @Autowired
    private FiTempCarAppStandService fiTempCarAppStandService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private TransportDispatchService transportDispatchService;

    @Autowired
    private TDispatchService tDispatchService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/fiTempCarAppStandExa.html";
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-11-19
     */
    @ResponseBody
    @RequestMapping("/listPage")
    public LayuiPageInfo listPage(TransportDispatchParam transportDispatchParam, String condition, String startTime, String endTime
            , Long operateId ) {
        User user = userService.getcurrentUser();
        transportDispatchParam.setDeptId(user.getDeptId());
        transportDispatchParam.setDeliverId(user.getDeliverId());
        String times = "";
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            times = "'" + startTime + "' and '" + endTime + "'";
        } else if (startTime == null && endTime == null) {
            startTime = DateUtil.getDate();
            endTime = DateUtil.getDate();
            times = "'" + startTime + "' and '" + endTime + "'";
        }
        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(operateId)){
            operateId = user.getOperateId();
        }
        transportDispatchParam.setApproverUser(user.getAccount());
        return this.transportDispatchService.findTmpCarAppExa(transportDispatchParam, condition, times, operateId, user.getOperateId());
    }


    /**
     * 审核
     * @param ids
     * @return
     */
    @RequestMapping("/shenhe")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,timeout = 60)
    public ResponseData shenhe(@RequestParam("ids") String ids) {
        if (ToolUtil.isEmpty(ids)) {
            return ResponseData.error("请选择需要审核的数据");
        }
        List<String> list = ListUtil.of(ids.split(","));

        //修改审核状态
        tDispatchService.lambdaUpdate()
                .in(TDispatch::getId,list)
                .set(TDispatch::getAppSt,"已审核")
                .update();
        return ResponseData.success();
    }


}


